/**********************************采购计划js方法库*****************************************/

/**
 * 请求采购计划数据
 */
// 异步加载采购计划
function loadpurchaseData(url, requsetData, callback) {
	var tbody = $("#planTable").find("tbody");
	// 清除该table的 tbody
	tbody.empty();
	$("#loading").show();
	post(url, requsetData, function(data) {
		var style = "";
		var page = data.data;
		if (data.resultCode == 100) {
			// 将数据插入table显示
			$.each(page.result, function(index, item) {
				tbody.append(getRow(item));
			});
			
			$("#loading").hide();
			$("#search_btn").attr("disabled", false);
			$("#refresh_btn").attr("disabled", false);
			getPageNation($("#pagination"), page.totalCount, page.pageSize,
					page.thisPageNumber, "queryPlan");
		}
	});
}
// 获取每行采购计划数据
function getRow(item) {
	var id = item.purchaseplanId;
	var trId = item.purchaseplanNum;
	var tr = $("<tr data-id='" + id + "' class='table" + trId + "'></tr>");
	var purchaseplanNum = $("<td>" + trId + "</td>");
	var purchaseplanName = $("<td>" + item.purchaseplanName + "</td>");
	var purchaseplanType = item.purchaseplanType;
	var typeName = "";
	if (purchaseplanType == 1) {
		typeName = $("<td data-type='1'>供应商供货</td>");
	} else {
		typeName = $("<td data-type='2'>自主采购</td>");
	}

	supplierName = $("<td>" + item.supplierName + "</td>");
	if (item.supplierName == null || item.supplierName == "")
		supplierName = $("<td></td>");
	var editorName = $("<td>" + item.editorName + "</td>");
	var createTime = $("<td>"
			+ new Date(parseInt(item.createTime)).format("yyyy-MM-dd hh:mm")
			+ "</td>");
	var purchaseplanStatus = item.planStatus;
	//alert(purchaseplanStatus);
	var status = "";
	var operation = "<td>";
	if (purchaseplanStatus == 2) {
		status = $("<td data-status='2'>采购完成</td>");
		operation = operation
				+ "<a href='"
				+ root
				+ "/purchaseinfo/"
				+ id
				+ "' ' target='_self' class='operation' >查看</a>&nbsp;&nbsp;"
				+ "<a href='javascript:void(0);' class='operation' onclick='submit_check(this,"
				+ id + ")'>提交质检</a></td>";
	} else if (purchaseplanStatus == 1) {
		status = $("<td data-status='1'>正在执行</td>");
		operation = operation
				+ "<a href='"
				+ root
				+ "/purchaseinfo/"
				+ id
				+ "' target='_self' class='operation'>添加采购记录</a>&nbsp;&nbsp;&nbsp;&nbsp;"
				+ "<a href='javascript:void(0);' class='operation' onclick='deleteplan(this,"
				+ id + ")'>删除</a></td>"
	} else if (purchaseplanStatus == 3) {
		status = $("<td data-status='3'>已提交</td>");
		operation = operation + "<a href='" + root + "/purchaseinfo/" + id
				+ "' ' target='_self' class='operation' >查看</a>";
	} else if (purchaseplanStatus == 4) {
		status = $("<td data-status='4'>正在计算</td>");
		operation = operation + "<a href='" + root + "/purchaseinfo/" + id
				+ "' ' target='_self' class='operation' >查看</a>";
	} else if (purchaseplanStatus == 5) {
		status = $("<td data-status='5'>已入库</td>");
		operation = operation + "<a href='" + root + "/purchaseinfo/" + id
				+ "' ' target='_self' class='operation' >查看</a>";
	}
	
	operate = $(operation);

	tr.append(purchaseplanNum);
	tr.append(purchaseplanName);
	tr.append(typeName);
	tr.append(supplierName);
	tr.append(editorName);
	tr.append(createTime);
	tr.append(status);
	tr.append(operate);

	return tr;
}

/**
 * 添加采购计划
 */
// 添加按钮 显示添加界面
$("#add_btn").click(function() {
	initform();
	$('#add_new_dialog').modal('toggle');
});
// 初始化采购计划表单
function initform() {
	var tr = $("#add_new_dialog");
	tr.find("input[type='text']").val("");
	tr.find("input[type='hidden']").val("");
	tr.find("select[name='purchaseplanSupplier']").empty();
	tr.find("select[name='purchaseplanType']").val("");
}

// 添加采购计划 校验表单
function checkTheForm() {
	var target = $("#add_new_dialog");

	var purchaseplanName = target.find("input[name='purchaseplanName']").val();
	var purchaseplanType = target.find("select[name='purchaseplanType']").val();
	var purchaseplanSupplier = target.find(
			"select[name='purchaseplanSupplier']").val();

	if (purchaseplanName == "") {
		alert("请输入采购计划名称！");
		return false;
	} else if (purchaseplanSupplier == "") {
		alert("请选择正确的负责人！");
		return false;
	} else if (purchaseplanType == "") {
		alert("请选择正确的采购类型！");
		return false;
	}
	return true;
}
// 提交添加采购计划表单
function submit_form(which) {
	var tr = $("#add_new_dialog");
	var purchaseplanName = tr.find("input[name='purchaseplanName']").val();
	var purchaseplanType = tr.find("select[name='purchaseplanType']").val();
	var purchaseplanSupplier = tr.find("select[name='purchaseplanSupplier']")
			.val();

	if (checkTheForm()) {
		data = JSON.stringify({
			purchaseplanName : purchaseplanName,
			purchaseplanType : purchaseplanType,
			purchaseplanSupplier : purchaseplanSupplier
		});
		var url = root + "/purchaseplan/asynsubmit";

		$(which).attr("disabled", true);
		$(which).text("保存中…");
		$.ajax({
			type : 'POST',
			url : url,
			contentType : 'application/json',
			data : data,
			dataType : 'json',
			success : function(data) {
				$(which).removeAttr("disabled");
				$(which).text("确认保存");
				$("#planTable").find("tbody").prepend(getRow(data.data));
				$('#add_new_dialog').modal('toggle');
				initform();
				// 获取与该负责人关联的原材料
				setRelationMaterial(data.data.purchaseplanId,
						data.data.purchaseplanType,
						data.data.purchaseplanSupplier);
			},
			error : function() {
				$(which).removeAttr("disabled");
				$(which).text("确认保存");
				alert("请求处理失败！");
			}
		});
	}
}

/**
 * 删除采购计划
 */
function deleteplan(which, id) {

	confirmDialog("您确定删除该采购计划吗？", function() {

	showLoading(which, true);
	$(which).hide();
	var url = root + "/purchaseplan/asyndelete";
	data = JSON.stringify({
		purchaseplanId : id
	});
	$.ajax({
		type : 'POST',
		url : url,
		contentType : 'application/json',
		data : data,
		dataType : 'text',
		success : function(data) {
			if (data == "success") {
				$(which).parent().parent().remove();
			} else {
				alert("删除失败,该采购计划下有已完成的采购，无法删除！");
				removeLoading(which);
				$(which).show();
			}
		},
		error : function() {
			alert("请求处理失败！");
		}
	});
	});

}

/**
 * 改变采购计划状态
 */
// 提交质检改变状态
function submit_check(which, id) {
	confirmDialog("您确定要提交质检吗？", function() {
	showLoading(which, true);
	$(which).hide();
	var status = 3;
	var url = root + "/purchaseplan/submitTocheck";
	data = JSON.stringify({
		planStatus : status,
		purchaseplanId : id
	});
	post(url, data, function(data) {
		if (data.resultCode == "100") {
			var tr = $(which).parent().parent();
			td = tr.children("td:eq(6)");
			td.empty();
			td.attr("data-status", 3);
			td.text("已提交");
			td = tr.children("td:eq(7)");
			td.empty();
			var operation = "<a href='" + root + "/purchaseinfo/" + id
					+ "' ' target='_blank' class='operation' >查看</a>";
			td.append(operation);
		}
	});
	});
}
// 点击“完成采购计划”按钮改变采购计划状态
function finish_purchase(which, id) {
	$(which).attr("disabled", true);
	$(which).text("计划完成中…");
	$(which)

	var url = root + "/purchaseplan/changeStatus";
	data = JSON.stringify({
		purchaseplanId : id
	});
	post(
			url,
			data,
			function(data) {
				if (data.resultCode == "100") {
					var tr = $(which).parent().parent();
					td = tr.children("td:eq(6)");
					td.empty();
					td.attr("data-status", 2);
					td.text("采购完成");
					td = tr.children("td:eq(7)");
					td.empty();
					operation = "<a href='"
							+ root
							+ "/purchaseinfo/"
							+ id
							+ "' ' target='_blank' class='operation' >查看</a>&nbsp;&nbsp;"
							+ "<a href='javascript:void(0);' class='operation' onclick='submit_check(this,"
							+ id + ")'>提交质检</a>";
					td.append(operation);
				}
				$(which).removeAttr("disabled");
				$(which).text("完成采购计划");
				queryInfoList();
			});
}

/**
 * 添加采购计划时选择不同采购方式对应负责人列表变化
 */
// 初始化加载下拉菜单
var supplierOptions = "";
var userOptions = "";

function iniselect() {
	supplierOptions = "<option value=''>选择供应商</option>";
	userOptions = "<option value=''>选择采购员</option>";
	$.each(suppliers, function(i, item) {
		supplierOptions += "<option value='" + item.supplierId + "'>"
				+ item.supplierName + "</option>";
	});
	$.each(users, function(i, item) {
		userOptions += "<option class='item' value='" + item.userId + "'>"
				+ item.userName + "</option>";
	});
	$("#purchaseplanSupplier").append($(supplierOptions));
	$("#purchaseplanSupplier").append($(userOptions));
}
// 采购类型变化，责任人列表变化
function setSelectOptions(value, select) {
	var options = $(supplierOptions);
	if (value == 2)
		options = $(userOptions);
	select.empty();
	select.append(options);
}
// 添加采购计划页面 采购类型下拉菜单
var purchaseType = 2;
$("#theHandleType").change(function() {
	iniselect();
	purchaseType = $(this).val();
	var select = $("#purchaseplanSupplier");

	setSelectOptions(purchaseType, select);
});

/**
 * 获取该供应商/采购员关联原材料
 */
function setRelationMaterial(purchaseplanId, purchaseplanType,
		purchaseplanSupplier) {
	var url = root + "/purchaseinfo/setRelationMaterial";
	data = JSON.stringify({
		purchaseplanId : purchaseplanId,
		purchaseplanType : purchaseplanType,
		purchaseplanSupplier : purchaseplanSupplier
	});
	post(url, data, function(data) {
		if (data.resultCode == "101")
			alert("请求失败！");
	});
}


/**********************************采购记录js方法库**************************************** */

// 异步加载采购详情记录
function loadpurchaseinfoData(url, requsetData, callback) {
	var tbody = $("#infoTable").find("tbody");
	// 清除该table的 tbody
	tbody.empty();
	$("#loading").show();
	post(url, requsetData, function(data) {
		var page = data.data;
		if (data.resultCode == 100) {
			// 将数据插入table显示
			$.each(page.result, function(index, item) {
				tbody.append(detailRow(item));
			});
			
			$("#loading").hide();
			$("#search_btn").attr("disabled", false);
			getPageNation($("#pagination"), page.totalCount, page.pageSize,
					page.thisPageNumber, "queryInfo");
		}
	});
}
// 获取采购记录每一行数据
function detailRow(item) {
	var tr = $("<tr></tr>");
	var checkbox = $("<td><input type='checkbox' name='items_" + purchaseplanId
			+ "' value='" + item.purchaseId + "'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>");
	var materialname = $("<td>" + item.materialName + "</td>");
	var materialtype = $("<td>" + item.materialType + "</td>");
	var plan = item.planQuantity;
	var unit = item.unit;
	if (plan == null) {
		plan = "";
		if(editAuth == 7)
			planQuantity = $("<td><div class='form-group'>"
					+ "<input type='text' name='input_plan' class='form-field col-1' style='text-align:center;' data-unit='"
					+ unit + "' value='" + plan + "'>" + "<span class='unit'>"
					+ unit + "</span></div></td>");
		else
			planQuantity = $("<td>" + plan + "</td>");
	} else
		planQuantity = $("<td>" + plan + unit + "</td>");
	var real = item.realQuantity;
	if (item.status == 0 || item.status == 1) {
		if (real == null)
			real = "";
		if(editAuth == 7)
			realQuantity = $("<td><div class='form-group'>"
					+ "<input type='text' id='input_real' name='input_real' onblur='checkNumber3();' class='form-field col-1' style='text-align:center;' data-unit='"
					+ unit + "' value='" + real + "'><span class='unit'>" + unit
					+ "</span></div></td>");
		else
			realQuantity = $("<td>" + real + "</td>");
	} else
		realQuantity = $("<td>" + real + unit + "</td>");
	var priceVal = item.price;
	var sumVal = item.sumPrice;
	var price = "";
	if (item.status == 0 || item.status == 1) {
		if (priceVal == null)
			priceVal = "";
		if(editAuth == 7)
			price = $("<td><div class='form-group'>"
					+ "<input type='text' id='input_price' name='input_price' onblur='checkNumber4();' class='form-field col-1' style='text-align:center;' value='"
					+ priceVal + "'><span class='unit'>元</span></div></td>");
		else
			price = statusName = $("<td>" + priceVal +"</td>");
	} else
		price = statusName = $("<td>" + parseFloat(priceVal).toFixed(2)
				+ "元</td>");

	var sum = "";
	if (sumVal == null)
		sum = $("<td></td>");
	else
		sum = $("<td>" + parseFloat(sumVal).toFixed(2) + "元</td>");

	var status = item.status;
	var operation = $("<td></td>");
	if (status == 1) {
		statusName = $("<td data-status='1'>正在执行</td>");
		if(editAuth == 7)
			operation = $("<td><a href='javascript:void(0);' class='operation' id='finish_btn_"
					+ item.purchaseId
					+ "' onclick='finish(this,"
					+ item.purchaseId
					+ ")'>完成采购</a><i class='icon-spinner icon-spin' style='display:none;'></i>"
					+ "&nbsp;&nbsp;&nbsp;<a href='javascript:void(0);' class='operation' id='delete_btn_"
					+ item.purchaseId
					+ "' onclick='deleteinfo(this,"
					+ item.purchaseId
					+ ")'>删除</a><i class='icon-spinner icon-spin' style='display:none;'></i></td>");

	} else if (status == 2) {
		statusName = $("<td data-status='2'>采购完成</td>");
		if(editAuth == 7)
			operation = $("<td><a href='javascript:void(0);' class='operation' id='edit_btn' onclick='editinfo(this,"
					+ item.purchaseId
					+ ")'>修改</a>"
					+ "<i class='icon-spinner icon-spin' style='display:none;'></i></td>");
	} else if (status == 3) {
		statusName = $("<td data-status='3'>已提交</td>");
	} else if (status == 0) {
		statusName = $("<td data-status='0'></td>");
		if(editAuth == 7)
			operation = $("<td><a href='javascript:void(0);' class='operation' id='plan_btn_"
					+ item.purchaseId
					+ "' onclick='makeplan(this,"
					+ item.purchaseId
					+ ")'>执行计划</a><i class='icon-spinner icon-spin' style='display:none;'></i>"
					+ "&nbsp;&nbsp;&nbsp;<a href='javascript:void(0);' class='operation' id='finish_btn_"
					+ item.purchaseId
					+ "' onclick='finish(this,"
					+ item.purchaseId
					+ ")'>完成采购</a><i class='icon-spinner icon-spin' style='display:none;'></i>"
					+ "&nbsp;&nbsp;&nbsp;<a href='javascript:void(0);' class='operation' id='delete_btn_"
					+ item.purchaseId
					+ "' onclick='deleteinfo(this,"
					+ item.purchaseId
					+ ")'>删除</a><i class='icon-spinner icon-spin' style='display:none;'></i></td>");
	}
	tr.append(checkbox);
	tr.append(materialname);
	tr.append(materialtype);
	tr.append(planQuantity);
	tr.append(realQuantity);
	tr.append(price);
	tr.append(sum);
	tr.append(statusName);
	tr.append(operation);
	return tr;
}

/**
 * 新增采购记录
 */
// 进入采购记录的新增
function addDetail(which, id) {
	var target = $(which).parent().parent().parent().find("#infoTable");
	var tr = target.children("tbody").prepend($("#addDetail"));
	$("#addDetail").show();
}

// 取消 添加计划详情 按钮事件
function cancelAdd(which) {
	initInputValue();
	$("#addDetail").hide();
	$(which).prev().prev().show();
	$(which).prev().hide();
}
// 保存新增采购记录
function confirmAdd(which, purchaseplanId) {
	var tr = $("#addDetail");
	var materialId = tr.find("input[name='materialId']").val();
	var materialName = tr.find("input[name='materialName']").val();
	var planQuantity = tr.find("input[name='planQuantity']").val();
	var realQuantity = tr.find("input[name='realQuantity']").val();
	var priceVal = tr.find("input[name='price']").val();
	var planType = tr.find("input[name='purchaseplanType']").val();

	// 校验数据
	if (materialId == "" || isNaN(materialId) || materialId <= 0) {
		alert("未选择正确商品！");
		$(which).show();
		$(which).next().hide();
		return;
	}
	if (planQuantity == "" || isNaN(planQuantity) || planQuantity < 0) {
		alert("计划采购量不正确！");
		$(which).show();
		$(which).next().hide();
		return;
	}

	data = JSON.stringify({
		purchaseplanId : purchaseplanId,
		materialId : materialId,
		planQuantity : planQuantity,
		realQuantity : realQuantity,
		price : priceVal
	});

	var url = root + "/purchaseinfo/asynAdd";

	$(which).hide(); // 隐藏按钮 防止重复提交
	$(which).next().show();
	post(
			url,
			data,
			function(data) {
				if (data.resultCode == "101")
					alert("添加的数据不符合要求！");
				else if (data.resultCode == "100") {
					// 新增一行 详情记录
					var info = data.info;
					var materialType = data.material;
					var material = data.materialInfo;
					var user = data.user;
					target = $(which).parent().parent().parent();
					var tr = target.children("tr:last");

					tr = tr.clone();
					td = tr.children("td").eq(0);
					td.empty();
					var checkbox = $("<input type='checkbox' name='items_"
							+ purchaseplanId + "' value='" + info.purchaseId
							+ "'>");
					td.append(checkbox);
					td = tr.children("td").eq(1);
					td.text(materialName);
					td = tr.children("td").eq(2);
					if (materialType == null)
						td.text("");
					else
						td.text(materialType.typeName);
					td = tr.children("td").eq(3);
					td.text(planQuantity + material.unit);
					td = tr.children("td").eq(4);
					if (realQuantity == null || realQuantity == "") {
						realQuantity = "";
						var real = $("<td><div class='form-group'>"
								+ "<input type='text' name='input_real' class='form-field col-1' style='text-align:center;' data-unit='"
								+ material.unit + "' value='" + realQuantity
								+ "'>" + "<span class='unit'>" + material.unit
								+ "</span></div><td>");
						td.empty()
						td.append(real);
					} else
						td.text(realQuantity + material.unit);

					td = tr.children("td").eq(5);
					if (priceVal == null || priceVal == "") {
						priceVal = "";
						var price = $("<div class='form-group'>"
								+ "<input type='text' name='input_price' class='form-field col-1' style='text-align:center;'value='"
								+ priceVal + "'>"
								+ "<span class='unit'>元</span></div>");
						td.empty()
						td.append(price);
					} else
						td.text(parseFloat(priceVal).toFixed(2) + "元");
					td = tr.children("td").eq(6);
					td.text(parseFloat(info.sumPrice).toFixed(2) + "元");
					
					td1 = tr.children("td").eq(7);
					td2 = tr.children("td").eq(8);
					td2.text("");
					if (info.status == 1) {
						td1.attr("data-status", "1");
						td1.text("正在执行");
						td2
								.append($("<a href='javascript:void(0);' class='operation' id='finish_btn_"
										+ info.purchaseId
										+ "' onclick='finish(this,"
										+ info.purchaseId
										+ ")'>完成采购</a><i class='icon-spinner icon-spin' style='display:none;'></i>"
										+ "&nbsp;&nbsp;&nbsp;<a href='javascript:void(0);' class='operation' id='delete_btn_"
										+ info.purchaseId
										+ "' onclick='deleteinfo(this,"
										+ info.purchaseId
										+ ")'>删除</a><i class='icon-spinner icon-spin' style='display:none;'></i>"));

					} else if (info.status == 2) {
						td1.attr("data-status", "2");
						td1.text("采购完成");
						td2
								.append($("<a href='javascript:void(0);' class='operation' id='edit_btn' onclick='editinfo(this,"
										+ info.purchaseId
										+ ")'>修改</a>"
										+ "<i class='icon-spinner icon-spin' style='display:none;'></i>"));
					} else if (info.status == 0) {
						td1.attr("data-status", "0");
						td1.text("");
					} else {
						td1.attr("data-status", "3");
						td1.text("已提交");
					}
					target.append(tr);
					initInputValue(); // 添加完成后 初始化输入框
				}
				// 回复显示按钮
				$(which).show();
				$(which).next().hide();
			}, {
				errorhandle : function() {
					alert("数据处理异常，请刷新页面重新操作！")
					$("#addDetail").find("a.operation").show();
					$("#addDetail").find("i").hide();
				}
			});

}
// 初始化添加采购记录状态
function initInputValue() {
	var tr = $("#addDetail");
	tr.find("input[type='text']").val("");
	tr.find("input[type='hidden']").val("");
	tr.find("input.success").removeClass("success");
	tr.find("a.operation").show();
	tr.find("i").hide();
	tr.find(".unit").text("");
};

/**
 * 编辑采购记录
 */
// 进入采购记录的编辑
function editinfo(which, purchaseId) {
	$(which).hide(); // 隐藏按钮 防止重复提交
	$(which).next().show();
	data = JSON.stringify({
		purchaseId : purchaseId
	});
	var url = root + "/purchaseinfo/editinfo";
	post(
			url,
			data,
			function(data) {
				var td = $(which).parent();
				var tr = $(which).parent().parent();
				var real = tr.children("td:eq(4)");
				var total = tr.children("td:eq(5)");
				var info = data.info;
				var material = data.material;

				var input1 = $("<div class='form-group'>"
						+ "<input type='text' name='input_real' class='form-field col-1' style='text-align:center;' data-unit='"
						+ material.unit + "' value='" + info.realQuantity
						+ "'>" + "<span class='unit'>" + material.unit
						+ "</span></div>");
				var input2 = $("<div class='form-group'>"
						+ "<input type='text' name='input_price' class='form-field col-1' style='text-align:center;' value='"
						+ info.price + "'>"
						+ "<span class='unit'>元</span></div>");

				var operation = $("<a href='javascript:void(0);' class='operation' id='edit_btn' onclick='finish(this,"
						+ purchaseId
						+ ")'>保存修改</a>"
						+ "<i class='icon-spinner icon-spin' style='display:none;'></i>");

				real.empty();
				total.empty();
				td.empty();

				real.append(input1);
				total.append(input2);
				td.append(operation);

			});

}

// 保存编辑后的采购记录
function finish(which, purchaseId) {
	$(which).hide(); // 隐藏按钮 防止重复提交
	$(which).next().show();

	var tr = $(which).parent().parent();
	var plan = tr.find("input[name='input_plan']").val();
	var real = tr.find("input[name='input_real']").val();
	var unit = tr.find("input[name='input_real']").attr("data-unit");
	var priceVal = tr.find("input[name='input_price']").val();
	var operation = $("<a href='javascript:void(0);' class='operation' id='edit_btn' onclick='editinfo(this,"
			+ purchaseId
			+ ")'>修改</a>"
			+ "<i class='icon-spinner icon-spin' style='display:none;'></i>");
	// 校验数据
	if (real == "" || isNaN(real) || real < 0) {
		$(which).show();
		$(which).next().hide();
		return false;
	}
	if (priceVal == "" || isNaN(priceVal) || priceVal < 0) {
		$(which).show();
		$(which).next().hide();
		return false;
	}
	if (plan == undefined)
		plan = null;
	data = JSON.stringify({
		purchaseId : purchaseId,
		realQuantity : real,
		price : priceVal,
		planQuantity : plan
	});
	var url = root + "/purchaseinfo/asynupdate";
	post(url, data,
			function(data) {
				if (data.resultCode == "success") {
					if (plan != "" && plan != undefined) {
						tr.children("td:eq(3)").empty();
						tr.children("td:eq(3)").text(plan + unit);
					}
					tr.children("td:eq(4)").empty();
					tr.children("td:eq(5)").empty();
					tr.children("td:eq(7)").empty();

					tr.children("td:eq(4)").text(real + unit);
					tr.children("td:eq(5)").text(
							parseFloat(priceVal).toFixed(2) + "元");
					tr.children("td:eq(6)").text(
							parseFloat(priceVal * real).toFixed(2) + "元");
					tr.children("td:eq(7)").attr("data-status", "2");
					tr.children("td:eq(7)").text("采购完成");
					tr.children("td:eq(8)").empty();
					tr.children("td:eq(8)").append(operation);

					$(which).hide();
					$(which).prev().hide();
					$(which).next().hide();
				} else
					alert("操作失败！");
			});
}

/**
 * 删除采购记录
 */
function deleteinfo(which, id) {
	showLoading(which, true);
	$(which).hide();
	var url = root + "/purchaseinfo/asyndelete";
	data = JSON.stringify({
		purchaseId : id
	});
	post(url, data, function(data) {
		$(which).parent().parent().remove();
	});
}
/**
 * 执行采购
 */
function makeplan(which, id) {
	$(which).hide(); // 隐藏按钮 防止重复提交
	$(which).next().show();
	var tr = $(which).parent().parent();
	var plan = tr.find("input[name='input_plan']").val();
	var unit = tr.find("input[name='input_real']").attr("data-unit");
	var operation = $("<a href='javascript:void(0);' class='operation' id='finish_btn_"
			+ id
			+ "' onclick='finish(this,"
			+ id
			+ ")'>完成采购</a><i class='icon-spinner icon-spin' style='display:none;'></i>"
			+ "&nbsp;&nbsp;&nbsp;<a href='javascript:void(0);' class='operation' id='delete_btn_"
			+ id
			+ "' onclick='deleteinfo(this,"
			+ id
			+ ")'>删除</a><i class='icon-spinner icon-spin' style='display:none;'></i>");
	// 校验数据
	if (plan == "" || isNaN(plan) || plan < 0) {
		$(which).show();
		$(which).next().hide();
		return false;
	}

	data = JSON.stringify({
		purchaseId : id,
		planQuantity : plan
	});

	var url = root + "/purchaseinfo/makePlan";
	post(url, data, function(data) {
		if (data.resultCode == "success") {
			tr.children("td:eq(3)").empty();
			tr.children("td:eq(3)").text(plan + unit);
			if (data.data.purchaseType == 1) {
				tr.children("td:eq(4)").find("input[name='input_real']").val(
						plan);
				tr.children("td:eq(4)").find("input[name='input_real']").attr(
						"data-unit", unit);
			}
			tr.children("td:eq(7)").attr("data-status", "1");
			tr.children("td:eq(7)").text("正在执行");
			tr.children("td:eq(8)").empty();
			tr.children("td:eq(8)").append(operation);

			$(which).hide();
			$(which).prev().hide();
			$(which).next().hide();
		} else
			alert("操作失败！");
	});
}

/**
 * 批量操作
 */
// 批量执行
function batch_plan(which, planId) {
	if (confirm('确定开始执行所选采购记录？')) {
		$(which).attr("disabled", true);
		var ids = getItemsValue(planId);
		for (var i = 0; i < ids.length; i++) {
			var btn = document.getElementById("plan_btn_" + ids[i]);
			makeplan(btn, ids[i]);
		}
		$(which).attr("disabled", false);
	}
}

// 批量完成采购
function batch_finish(which, planId) {
	if (confirm('确定批量完成所选采购记录？')) {
		$(which).attr("disabled", true);
		var ids = getItemsValue(planId);
		for (var i = 0; i < ids.length; i++) {
			var btn = document.getElementById("finish_btn_" + ids[i]);
			finish(btn, ids[i]);
		}
		$(which).attr("disabled", false);
		setAllCheckboxState(planId);
	}
}

// 批量删除
function batch_delete(which, planId) {
	if (confirm('确定删除所选采购记录？')) {
		$(which).attr("disabled", true);
		var ids = getItemsValue(planId);
		for (var i = 0; i < ids.length; i++) {
			var btn = document.getElementById("delete_btn_" + ids[i]);
			var status = $(btn).parent().parent().children("td:eq(7)").attr(
			"data-status");
			if (status==0 || status == 1)
				deleteinfo(btn, ids[i]);
		}
		$(which).attr("disabled", false);
	}
}
// 获取选中的值,进行批量完成/执行
function getItemsValue(planId) {
	var elements = document.getElementsByName("items_" + planId);
	var ids = new Array();
	for (var i = 0; i < elements.length; i++)
		if (elements[i].checked)
			ids[i] = elements[i].value;

	if (ids.length == 0) {
		alert("请选择相关记录！");
	}
	return ids;
}

/**********************************其他js方法库**************************************** */
/**
 * 加载
 */
// 显示正在加载
function showLoading(which, before) {
	var spinner = $("<i class='icon-spinner icon-spin'></i>");
	if (before)
		spinner.insertBefore($(which));
	else
		spinner.insertAfter($(which));
}
// 去除正在加载
function removeLoading(which) {
	$(which).siblings(".icon-spinner").remove();
}

/**
 * checkbox全选或全不选
 */
function setAllCheckboxState(planId) {
	var flag = document.getElementById("flag_" + planId).checked;
	var elements = document.getElementsByName("items_" + planId);
	for (var i = 0; i < elements.length; i++) {
		if (elements[i].type != 'checkbox') {
			continue;
		} else {
			elements[i].checked = flag;
		}
	}
}
//搜索框清空
$("#remove_icon").click(function(){
	$("#remove_icon").hide();
	$("#remove_icon").siblings("input").val("");
});